package cn.ac.jaas.isc.vegetable.pbms.service.impl;

import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.security.service.VegetableUser;
import cn.ac.jaas.isc.vegetable.common.security.util.SecurityUtils;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipment;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipmentType;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcEquipmentTypeMapper;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcEquipmentTypeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备类型表
 *
 * @author 张高昌
 * @date 2022-05-14 16:34:43
 */
@Slf4j
@Service
@AllArgsConstructor
public class ArcEquipmentTypeServiceImpl extends ServiceImpl<ArcEquipmentTypeMapper, ArcEquipmentType> implements ArcEquipmentTypeService {

    private final ArcEquipmentMapper arcEquipmentMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R saveEquipmentType(ArcEquipmentType arcEquipmentType) {

        ArcEquipmentType nameModel = baseMapper.selectOne(Wrappers.<ArcEquipmentType>query().lambda().eq(ArcEquipmentType::getEquipmentName, arcEquipmentType.getEquipmentName()));
        if (nameModel != null) {
            return R.failed("设备类型名称已存在！");
        }
        ArcEquipmentType codeModel = baseMapper.selectOne(Wrappers.<ArcEquipmentType>query().lambda().eq(ArcEquipmentType::getEquipmentTypeCode, arcEquipmentType.getEquipmentTypeCode()));
        if (codeModel != null) {
            return R.failed("设备类型编码已存在！");
        }
        VegetableUser user = SecurityUtils.getUser();
        arcEquipmentType.setCreateTime(LocalDateTime.now());
        arcEquipmentType.setCreateUser(user.getId());
        baseMapper.insert(arcEquipmentType);

        return R.ok(true);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateEquipmentTypeById(ArcEquipmentType arcEquipmentType) {
        LambdaQueryWrapper<ArcEquipmentType> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ArcEquipmentType::getEquipmentName, arcEquipmentType.getEquipmentName())
                .ne(ArcEquipmentType::getEquipmentTypeId, arcEquipmentType.getEquipmentTypeId());
        ArcEquipmentType nameModel = baseMapper.selectOne(wrapper);
        if (nameModel != null) {
            return R.failed("设备类型名称已存在！");
        }
        LambdaQueryWrapper<ArcEquipmentType> typeWrapper = Wrappers.lambdaQuery();
        typeWrapper.eq(ArcEquipmentType::getEquipmentTypeCode, arcEquipmentType.getEquipmentTypeCode())
                .ne(ArcEquipmentType::getEquipmentTypeId, arcEquipmentType.getEquipmentTypeId());
        ArcEquipmentType codeModel = baseMapper.selectOne(typeWrapper);
        if (codeModel != null) {
            return R.failed("设备类型编码已存在！");
        }
        VegetableUser user = SecurityUtils.getUser();
        arcEquipmentType.setUpdateTime(LocalDateTime.now());
        arcEquipmentType.setUpdateUser(user.getId());
        baseMapper.updateById(arcEquipmentType);

        return R.ok(true);
    }

    @Override
    public R removeEquipmentTypeById(Integer equipmentTypeId) {
        ArcEquipmentType arcEquipmentType = baseMapper.selectById(equipmentTypeId);
        List<ArcEquipment> arcEquipments = arcEquipmentMapper.selectList(Wrappers.<ArcEquipment>query().lambda().eq(ArcEquipment::getEquipmentTypeCode, arcEquipmentType.getEquipmentTypeCode()));
        if (arcEquipments.size() > 0) {
            return R.failed("该类型已在使用中，无法删除");
        }
        baseMapper.deleteById(equipmentTypeId);
        return R.ok(true);
    }
}
